define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'bookscategory/index' + location.search,
                    add_url: 'bookscategory/add',
                    edit_url: 'bookscategory/edit',
                    del_url: 'bookscategory/del',
                    multi_url: 'bookscategory/multi',
                    import_url: 'bookscategory/import',
                    table: 'books_category',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'level', title: __('Level'), searchList: {"1":__('Level 1'),"2":__('Level 2')}, formatter: Table.api.formatter.normal},
                        {field: 'pcategory.name', title: '上级分类', formatter: Table.api.formatter.normal},
                        {field: 'images', title: __('Images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },


        add: function () {
            Controller.api.bindevent();
            // 切换单/多规格
            $('select[name="row[level]"]').change(function (e) {
                var $pid        = $('.pid');
                console.log(e.currentTarget.value);
                if (e.currentTarget.value === '2') {
                    $pid.show();
                }
                if(e.currentTarget.value === '1') {
                    $pid.hide();
                }
            });
        },
        edit: function () {
            Controller.api.bindevent();
            // 切换单/多规格
            $('select[name="row[level]"]').change(function (e) {
                var $pid        = $('.pid');

                if (e.currentTarget.value === '2') {
                    $pid.show();
                }
                if(e.currentTarget.value === '1') {
                    $pid.hide();
                }
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
